package com.mice.game.core
{
	import com.mice.conf.FighterType;
	import com.mice.meta.FighterMeta;
	import com.mice.meta.MetaManager;
	
	import flash.display.MovieClip;

	public class Fighter extends Actor
	{
		/**
		 * 生命值
		 */
		protected var _life:int;
		
		/**
		 * 正在攻击的人
		 */		
		private var _enemy:Fighter;
		
		public function Fighter(){}
		
		public function get meta():FighterMeta{
			return MetaManager.fighterMeta[_level][_metaId] as FighterMeta;
		}
		
		override public function initialize(metaId:int, level:int):void{
			super.initialize(metaId, level);
			
			_life = meta.maxLife;
			_speed = meta.speed;
			
			var cl:Class = FighterType.styleDict[meta.styleName] as Class;
			_style = new cl() as MovieClip;
			_style.y = 285;
			_style.gotoAndStop(1);
		}
		
		/**
		 * 前进
		 */		
		public function step():void{
			if (_status == STATUS_RUN){
				x += _speed;
				_style.x = x;
			}
		}
		
		/**
		 * 被打
		 * @param value 攻击力
		 */		
		public function underAttack(value:int):void{
			_life -= value;
			if (_life <= 0)
				setStatus(STATUS_DEAD);
		}
		
		/**
		 * 第一次攻击
		 * @param enemy
		 */		
		public function attack(enemy:Fighter):void{
			_enemy = enemy;
			setStatus(STATUS_ATTACK);
			continueAttack();
		}
		
		/**
		 * 消除冷却,下次攻击
		 */		
		public function waitToContinueAttack():void{
			_currAttackInternal--;
			if (_currAttackInternal <= 0)
				continueAttack();
		}
		
		/**
		 * 已经有目标,继续攻击
		 */		
		private function continueAttack():void{
			if (_enemy.isDead){
				_enemy = null;
				setStatus(STATUS_RUN);
				return;
			}
			
			_currAttackInternal = meta.attackInternal;
			playAttackAnimation();
			_enemy.underAttack(meta.attack);
		}
		
		/**
		 * enemy是否在自己的攻击范围内
		 * @param enemy
		 */		
		public function testAttackRange(enemy:Fighter):Boolean{
			var dis:int = Math.abs(x - enemy.x);
			return dis <= meta.attackRange * 100;
		}
		
		/**
		 * 播放攻击动画
		 */		
		private function playAttackAnimation():void{
			_style.gotoAndStop(2);
		}
		
		/**
		 * 设置行为状态,并更新相关数据
		 */		
		private function setStatus(value:int):void{
			_status = value;
			if (_style.totalFrames == 1)
				return;
			
			if (_status == STATUS_DEAD){
				_style.parent.removeChild(_style);
				_style.stop();
				_style = null;
			}else if (_status == STATUS_ATTACK){
				_style.gotoAndStop(2);
			} else if (_status == STATUS_RUN){
				_style.gotoAndStop(1);
			}
		}
	}
}